package com.wnx.action;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.wnx.db.GoodsDao;
import com.wnx.db.SchoolDao;
import com.wnx.entity.Goods;
import com.wnx.entity.Menu;
import com.wnx.entity.School;
import com.wnx.service.SaleService;

public class GoodsAction extends ActionSupport {
	private Goods g;
	private File myFile;  
	public File getMyFile() {  
        return myFile;  
    }  
    public void setMyFile(File myFile) {  
        this.myFile = myFile;  
    }  
  
    private String myFileContentType;  
  
    private String myFileFileName;  
  
    private String destPath;  
	public Goods getG() {
		return g;
	}

	public void setG(Goods g) {
		this.g = g;
	}

    public String execute()  
    {  
         /* Copy file to a safe location */  
        destPath = ServletActionContext.getServletContext().getRealPath("/");
        File f = new File(destPath);
        f = new File(f.getParent(),"files/SecondaryMarket");
        destPath = f.getAbsolutePath();
        try{  
        	String name = UUID.randomUUID().toString().replaceAll("-", "");
        	int index = myFileFileName.lastIndexOf(".");
    		String suffix = myFileFileName.substring(index, myFileFileName.length());
            System.out.println("Src File name: " + myFile);  
            System.out.println("Dst File name: " + name + suffix);  
            File destFile  = new File(destPath, name + suffix);  
            FileUtils.copyFile(myFile, destFile);
            Goods good = new Goods();
    		good.setName(g.getName());
    		good.setIntroduce(g.getIntroduce());
    		good.setImages("/files/SecondaryMarket/"+name+suffix);
    		good.setSid(g.getSid());
    		good.setMid(g.getMid());
    		good.setAddress(g.getAddress());
    		good.setUid(g.getUid());
    		good.setPhone(g.getPhone());
    		good.setQq(g.getQq());
    		good.setPrice(g.getPrice());
    		good.setClick(0);
    		good.setState(0);
    		Date now = new Date(); 
    		SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd");
    		String date = sdf.format( now ); 
    		good.setDate(date);
    		GoodsDao gd = new GoodsDao();
    		gd.saveG(good);
        }catch(IOException e){  
            e.printStackTrace();  
            return ERROR;  
        }  
        return SUCCESS;  
    }  
  
	   public String getMyFileContentType() {  
	        return myFileContentType;  
	    }  
	    public void setMyFileContentType(String myFileContentType) {  
	        this.myFileContentType = myFileContentType;  
	    }  
	    public String getMyFileFileName() {  
	        return myFileFileName;  
	    }  
	    public void setMyFileFileName(String myFileFileName) {  
	        this.myFileFileName = myFileFileName;  
	    } 
}
